package com.tuyan.aixiang.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import android.text.TextUtils;

import com.tuyan.aixiang.MainApplication;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.UUID;

public class DeviceUtil {

    /**
     * 获取设备的唯一标识，deviceId
     *
     * @param context
     * @return
     */
    public static String getDeviceId(Context context) {
//        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
//        String deviceId = tm.getDeviceId();

        SharedPreferences sp = context.getSharedPreferences("by_device_uuid", Context.MODE_PRIVATE);
        String uuid = sp.getString("uuid", "");
        if (TextUtils.isEmpty(uuid)) {
            uuid = UUID.randomUUID().toString();
            SharedPreferences.Editor editor = sp.edit();
            editor.putString("uuid", uuid);
            editor.commit();
        }
        if (uuid == null) {
            return "";
        } else {
            return uuid;
        }
    }

    public static String getUserAgent() {
        try {
            StringBuilder builder = new StringBuilder();
            builder.append("baoyou");
            builder.append("(Android;");
            builder.append(Build.VERSION.RELEASE).append(";");
            builder.append(URLEncoder.encode(Build.MANUFACTURER, "UTF-8")).append(";");
            builder.append(URLEncoder.encode(Build.MODEL, "UTF-8")).append(";");
            builder.append(SystemUtil.getVersionName(MainApplication.getAppContext())).append("(").append(SystemUtil.getVersionCode(MainApplication.getAppContext())).append("))");
            return builder.toString();
        } catch (UnsupportedEncodingException e) {
            return "baoyou(;;;Android" + SystemUtil.getVersionName(MainApplication.getAppContext()) + ")";
        }
    }
}
